package hong.jun.pet_home.service.pet.impl;

import hong.jun.basic.base.service.impl.BaseServiceImpl;
import hong.jun.basic.util.DistanceUtil;
import hong.jun.pet_home.domain.pet.Adopt;
import hong.jun.pet_home.domain.pet.Pet;
import hong.jun.pet_home.domain.org.Shop;
import hong.jun.pet_home.domain.pet.PetDetail;
import hong.jun.pet_home.domain.util.Point;
import hong.jun.pet_home.mapper.AdoptMapper;
import hong.jun.pet_home.mapper.PetDetailMapper;
import hong.jun.pet_home.mapper.PetMapper;
import hong.jun.pet_home.mapper.ShopMapper;
import hong.jun.pet_home.query.ShopQuery;
import hong.jun.pet_home.service.pet.IAdoptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdoptServiceImpl extends BaseServiceImpl<Adopt> implements IAdoptService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private AdoptMapper adoptMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    public void publish(Adopt adopt) {
        //获取宠物所在地址经纬度
        String address = adopt.getAddress();
        Point point = DistanceUtil.getPoint(address);

        //获取同一座城市的店铺
        String substring = address.substring(0, address.indexOf("市") + 1);
        ShopQuery shopQuery = new ShopQuery();
        shopQuery.setKeywords(substring);
        List<Shop> all = shopMapper.loadAlladdress(shopQuery);

        // 获取最近店铺
        Shop nearestShop = DistanceUtil.getNearestShop(point, all);
        // 把寻主消息绑定给店铺
        adopt.setShop(nearestShop);

        System.out.println(adopt);
        adoptMapper.save(adopt);
    }

    /**
     * 入库
     */
    @Override
    public void Warehousing(Adopt adopt) {
        adopt.setState(1);//改变状态为入库
        super.update(adopt);
        Pet pet = adopttopet(adopt);
        System.out.println(pet);
        petMapper.save(pet);
        PetDetail petDetail = new PetDetail();
        petDetail.setPet_id(pet.getId());
        petDetailMapper.save(petDetail);
    }

    /**
     * adopt转换pet
     */
    public Pet adopttopet(Adopt adopt) {
        Pet pet = new Pet();
        pet.setName(adopt.getName());  //名称
        pet.setAge(adopt.getAge());//年龄
        pet.setCoat_color(adopt.getCoat_color());//毛色
        pet.setCostprice(adopt.getPrice());//收购售价
        pet.setGender(adopt.getGender());//性别
        pet.setState(0);//状态 -1不好 0正常 1上架 2被收养
        pet.setCreatetime(new Date()); //创建时间
        pet.setResources(adopt.getResources()); //图片资源

        pet.setAdopt_id(adopt.getId());   //所属原订单id
        pet.setShop_id(adopt.getShop().getId());  //处理店铺id
        pet.setType_id(adopt.getPettype().getId());//宠物类型id

        //领养售价
        //下架时间
        //上架时间
        //所属主人id
        return pet;
    }
}
